using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Entities;
using Bll;

public partial class Service_SidebarSearchControl : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }        
    }

    private void ReturnSession(IList<ProductInfo> proList)
    {      
        HttpContext.Current.Session["SearchResult"] = proList;
        Response.Redirect("searchresults.aspx");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ProductBll proBll = new ProductBll();

        if (HttpContext.Current.Session["SearchResult"] != null)
        {
            HttpContext.Current.Session.Remove("SearchResult");
        }

        if (cbCategory.Checked == false)
        {
            if (cbSearchByPrice.Checked == false)
            {
                IList<ProductInfo> proList = proBll.GetProductByAllCategoryAndSearchkey(txtsearch.Text);
                ReturnSession(proList);
            }
            else
            {
                IList<ProductInfo> proList = proBll.GetProductByAllCategoryAndPriceAndSearchkey(txtsearch.Text, txtPricefrom.Text, txtPriceto.Text);
                ReturnSession(proList);
            }
        }
        else
        {
            if (cbSearchByPrice.Checked == false)
            {
                try
                {
                    IList<ProductInfo> proList = proBll.GetProductByCategoryAndSearchkey(txtsearch.Text, int.Parse(ddlSearchByCategory.SelectedValue));
                    ReturnSession(proList);
                }
                catch (System.FormatException ex)
                {
                    if (ex.Message != null)
                    {
                        Session.Remove("SearchResult");
                        Response.Redirect("searchresults.aspx");
                    }
                }

            }
            else
            {
                if (string.IsNullOrEmpty(txtsearch.Text))
                {
                    if (txtPricefrom.Text.Length > 0 && txtPriceto.Text.Length > 0)
                    {
                        IList<ProductInfo> proList = proBll.GetProductByAllCategoryAndPrice(txtPricefrom.Text, txtPriceto.Text);
                        ReturnSession(proList);
                    }
                    else
                    {
                        Session.Remove("SearchResult");
                        Response.Redirect("searchresults.aspx");
                    }
                }
                else
                {
                    try
                    {
                        if (txtPricefrom.Text.Length > 0 && txtPriceto.Text.Length > 0)
                        {
                            IList<ProductInfo> proList = proBll.GetProductByCategoryAndPriceAndSearchkey(txtsearch.Text, txtPricefrom.Text, txtPriceto.Text, int.Parse(ddlSearchByCategory.SelectedValue));
                            ReturnSession(proList);
                        }
                        else
                        {
                            Session.Remove("SearchResult");
                            Response.Redirect("searchresults.aspx");
                        }
                    }
                    catch (System.FormatException ex)
                    {
                        if (ex.Message != null)
                        {
                            Session.Remove("SearchResult");
                            Response.Redirect("searchresults.aspx");
                        }
                    }

                }
            }
        }





        //if (string.IsNullOrEmpty(txtsearch.Text))
        //{
        //    if (string.IsNullOrEmpty(txtPricefrom.Text) || string.IsNullOrEmpty(txtPriceto.Text))
        //    {
        //        lblError.Text = "Input Information incorrect";
        //    }
        //    else
        //    {
        //        if (cbCategory.Checked == false)
        //        {
        //            if (cbSearchByPrice.Checked == true)
        //            {
        //                proList = new List<ProductInfo>();
        //                proList = proBll.GetProductByAllCategoryAndPrice(txtPricefrom.Text, txtPriceto.Text);
        //                HttpContext.Current.Session["SearchResult"] = proList;
        //                Response.Redirect("searchresults.aspx");
        //            }
        //            else
        //            {
        //                lblError.Text = "Tick Search By Price";
        //            }
        //        }
        //        else
        //        {
        //            if (cbSearchByPrice.Checked == true)
        //            {
        //                proList = new List<ProductInfo>();
        //                proList = proBll.GetProductByCategoryAndPrice(txtPricefrom.Text, txtPriceto.Text, int.Parse(ddlSearchByCategory.SelectedValue));
        //                HttpContext.Current.Session["SearchResult"] = proList;
        //                Response.Redirect("searchresults.aspx");
        //            }
        //            else
        //            {
        //                lblError.Text = "Tick Search By Price";
        //            }
        //        }
        //    }
        //}
        //else
        //{
        //    if (cbCategory.Checked == false)
        //    {
        //        if (cbSearchByPrice.Checked == false)
        //        {
        //            proList = new List<ProductInfo>();
        //            proList = proBll.GetProductByAllCategoryAndSearchkey(txtsearch.Text);
        //            HttpContext.Current.Session["SearchResult"] = proList;
        //            Response.Redirect("searchresults.aspx");
        //        }
        //        else
        //        {
        //            if (string.IsNullOrEmpty(txtPricefrom.Text) || string.IsNullOrEmpty(txtPriceto.Text))
        //            {
        //                lblError.Text = "Input Price";
        //            }
        //            else
        //            {
        //                proList = new List<ProductInfo>();
        //                proList = proBll.GetProductByAllCategoryAndPriceAndSearchkey(txtsearch.Text, txtPricefrom.Text, txtPriceto.Text);
        //                HttpContext.Current.Session["SearchResult"] = proList;
        //                Response.Redirect("searchresults.aspx");
        //            }
        //        }
        //    }
        //    else
        //    {
        //        if (cbSearchByPrice.Checked == false)
        //        {
        //            proList = new List<ProductInfo>();
        //            proList = proBll.GetProductByCategoryAndSearchkey(txtsearch.Text,int.Parse(ddlSearchByCategory.SelectedValue));
        //            HttpContext.Current.Session["SearchResult"] = proList;
        //            Response.Redirect("searchresults.aspx");
        //        }
        //        else
        //        {
        //             if (string.IsNullOrEmpty(txtPricefrom.Text) || string.IsNullOrEmpty(txtPriceto.Text))
        //            {
        //                lblError.Text = "Input Price";
        //            }
        //            else
        //            {
        //                proList = new List<ProductInfo>();
        //                proList = proBll.GetProductByCategoryAndPriceAndSearchkey(txtsearch.Text,txtPricefrom.Text,txtPriceto.Text,int.Parse(ddlSearchByCategory.SelectedValue));
        //                HttpContext.Current.Session["SearchResult"] = proList;
        //                Response.Redirect("searchresults.aspx");
        //            }
        //        }
        //    }
        //}


    }

    protected void cbCategory_CheckedChanged(object sender, EventArgs e)
    {
        if (cbCategory.Checked == true)
        {            
            ddlSearchByCategory.Enabled = true;
            txtsearch.Text = "";
        }
        else
        {
            ddlSearchByCategory.Enabled = false;
        }
    }
    protected void cbSearchByPrice_CheckedChanged(object sender, EventArgs e)
    {
        if (cbSearchByPrice.Checked == true)
        {
            txtPricefrom.Enabled = true;
            txtPriceto.Enabled = true;
            txtsearch.Text = "";
        }
        else
        {
            txtPricefrom.Enabled = false;
            txtPriceto.Enabled = false;
        }
    }
    protected void btnQuickSearch_Click(object sender, EventArgs e)
    {
        if (RadComboBox1.SelectedValue.Length > 0)
        {
            ProductBll proBll = new ProductBll();
            IList<ProductInfo> proList = new List<ProductInfo>();
            ProductInfo product = proBll.GetByID(RadComboBox1.SelectedValue);
            proList.Add(product);
            ReturnSession(proList);
        }
        else
            Session.Remove("SearchResult");
            Response.Redirect("searchresults.aspx");

    }
}
   
